package com.nightpiter.controller.reflection;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import android.content.Context;
import android.dalvik.DexFile;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public class Pack {

	/** Application context */
	private Context context;

	/** file represented in this object */
	private DexFile dexFile;

	/** the manifest name */
	private String packName;

	/** comment to this manifest */
	private String comment;

	/** authors' names */
	private ArrayList<String> authors;

	/** modules */
	private ArrayList<Module> modules = new ArrayList<Module>();

	/**
	 * public constructor
	 * 
	 * @param context
	 * @param dexFile
	 * @throws PackException
	 */
	public Pack(Context context, File file) throws PackException {
		this.context = context;
		try {
			this.dexFile = new DexFile(file);

			ZipFile zipFile = new ZipFile(file);
			ZipEntry zipEntry = zipFile.getEntry("manifest.xml");
			if (zipEntry == null) {
				throw new PackException("missing manifest.xml");
			}
			InputStream inputStream = zipFile.getInputStream(zipEntry);

			PackBuilder builder = new PackBuilder(this);

			PackReader.read(inputStream, builder);

			builder.build();
		} catch (Exception e) {
			throw new PackException(e);
		}
	}

	/**
	 * @param name
	 * @param comment
	 * @param authors
	 */
	void build(String name, String comment, ArrayList<String> authors) {
		this.packName = name;
		this.comment = comment;
		this.authors = authors;
	}

	/**
	 * add a module to the list
	 * 
	 * @param module
	 */
	void addModule(Module module) {
		this.modules.add(module);
	}

	/**
	 * @return the packName
	 */
	public String getPackName() {
		return packName;
	}

	/**
	 * @return the comment
	 */
	public String getComment() {
		return comment;
	}

	/**
	 * @return the authors
	 */
	public ArrayList<String> getAuthors() {
		return authors;
	}

	/**
	 * @return the modules
	 */
	public ArrayList<Module> getModules() {
		return modules;
	}

	/**
	 * @return the dexFile
	 */
	public DexFile getDexFile() {
		return dexFile;
	}

	/**
	 * @return the context
	 */
	public Context getContext() {
		return context;
	}

}
